﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
namespace com.huigo.Admin.Web.invoice
{
    public partial class InvoiceList : BasePage
    {
        #region 查询字段
        private string queryInvoiceOrderNo//订单编号
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["InvoiceorderNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["InvoiceorderNo"]);
            }
        }
        private string queryInvoiceCustName//经销商名字
        {
            get { return string.IsNullOrEmpty(Request.QueryString["InvoiceCustName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["InvoiceCustName"].ToString()); }
        }
        private DateTime queryInvoiceStart//开始时间
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["InvoiceStart"]) ? Convert.ToDateTime(Request.QueryString["InvoiceStart"]) : DateTime.MinValue;
            }
        }
        private DateTime queryInvoiceEnd//结束时间
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["InvoiceEnd"]) ? Convert.ToDateTime(Request.QueryString["InvoiceEnd"]) : DateTime.MinValue;
            }
        }
        private long queryInvoiceOrderType//订单状态
        {
             get
            {
                return string.IsNullOrEmpty(Request.QueryString["InvoiceOrderType"]) ? int.MinValue : Convert.ToUInt16(com.loto.Common.Utils.SqlEscape(Request.QueryString["InvoiceOrderType"]));
            }
        }
        private string queryInvoiceProName//保险产品
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["InvoiceProName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["InvoiceProName"]);
            }
        }
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                BindData();
            }
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "InvoiceList.aspx?";
            url += "page=1";
            if (orderName.Text != string.Empty)
                url += "&InvoiceorderNo=" +orderName.Text;
            if (custName.Text != string.Empty)
                url += "&InvoiceCustName=" +custName.Text;
            if (beginDate.Text != string.Empty)
                url += "&InvoiceStart=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&InvoiceEnd=" + endDate.Text;
            if (statusList.SelectedValue != string.Empty)
                url += "&InvoiceOrderType=" + statusList.SelectedValue;
            if (proList.SelectedValue != string.Empty)
                url += "&InvoiceProName=" + proList.SelectedValue;
            Response.Redirect(url);

        }
        private void LoadData()
        {
            //订单状态
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictInvoiceOrderStatus);
            if (statusDropList != null)
            {
                statusList.DataSource = statusDropList;
                statusList.DataValueField = "Id";
                statusList.DataTextField = "Value";
                statusList.DataBind();
            };
              List<RequestFuncParams> argList = new List<RequestFuncParams>();
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_PRODUCT_LIST_NAME, argList);

            List<DropList> cDropList = Tools.ParserToDropList(parser, "ProductId", "ProductName");
            if (cDropList != null)
            {
                proList.DataSource = cDropList;
                proList.DataValueField = "Id";
                proList.DataTextField = "Value";
                proList.DataBind();
            }
        }

        private void BindData()
        {
            orderName.Text = queryInvoiceOrderNo;
            custName.Text = queryInvoiceCustName;
            if (queryInvoiceStart.ToString("yyyy-MM-dd") != string.Empty && queryInvoiceEnd.ToString("yyyy-MM-dd") != string.Empty)
            {
                if (queryInvoiceStart.ToString("yyyy-MM-dd") != "0001-01-01" && queryInvoiceEnd.AddDays(1).ToString("yyyy-MM-dd") != "0001-01-02")
                {
                    beginDate.Text = queryInvoiceStart.ToString("yyyy-MM-dd");
                    endDate.Text = queryInvoiceEnd.ToString("yyyy-MM-dd");
                }
            }
            statusList.Text = queryInvoiceOrderType.ToString();
            proList.Text = queryInvoiceProName;

            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("InvoiceOrderNO",queryInvoiceOrderNo));
            argList.Add(new RequestFuncParams("InvoiceOrderStart", queryInvoiceStart.ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("InvoiceOrderEnd", queryInvoiceEnd.AddDays(1).ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("custName", queryInvoiceCustName));
            argList.Add(new RequestFuncParams("InvoiceOrderStatus", queryInvoiceOrderType.ToString()));
            argList.Add(new RequestFuncParams("proName", queryInvoiceProName));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService service = new BaseService();
            ParserXml xml = service.RequestEntryService(Dict.ERequestFunc.GET_INVOICE_ORDER_INFO, argList);
            StringBuilder sb = new StringBuilder();
            sb.Append("<table><thead><tr><th>订单号</th><th >经销商</th><th style=\"width: 20%\">保险产品名称</th><th >张数</th><th>配送方式</th><th>订单总金额</th><th>实发张数</th><th>物流公司</th><th>物流单号</th><th width=\"15%\">订单时间</th><th>订单状态</th><th>操作</th></tr></thead><tbody>");
            string content = "<tr><td>{0}</td><td><a href=\"/order/QueryPolicyByInvoice.aspx?orderID={1}&InsType={13}&keepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=850\" title=\"经销商-{2}\" class=\"thickbox\">{2}</a></td><td/>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td><div class=\"wrap_layout_track\"><a class=\"js_invoice_status_history\"  href=\"javascript:void(0);\" invoice_id=\"{1}\" >{11}<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>发票订单操作记录</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div></td><td>{12}</td></tr>";
            if (xml != null && xml.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(xml["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                for (int i = 0; i < xml.RowsCount; i++)
                {
                    sb.AppendFormat(content, xml["OrderNo", i].ToString(), xml["OrderId", i].ToString(), xml["CustName", i].ToString(), xml["OrderProductName", i].ToString(), xml["OrderPaperCount", i].ToString(), DeliverType(Convert.ToInt32(xml["OrderDeliverType", i]), Convert.ToInt64(xml["OrderId", i].ToString())), ConvertCurrencyPay(Convert.ToDecimal(xml["OrderPayAmount", i].ToString())), xml["OrderRealPaperCount", i], xml["OrderFedex", i], xml["OrderFedexNo", i], Convert.ToDateTime(xml["OrderCreatedOn", i]).ToString("yyyy-MM-dd HH:mm"), xml["StatusDesc", i].ToString(), ExitBtn(Convert.ToInt32(xml["OrderStatusFid", i]), Convert.ToInt64(xml["OrderId", i].ToString())), xml["InsType", i]);
                }
                sb.Append("</tbody></table>");
                LitInvoiceOrder.Text = sb.ToString();
            }
            else
            {
                LitInvoiceOrder.Text = string.Empty;
            }
        }
        private string DeliverType(int deliverType,long orderId)
        {
            string Meno = string.Empty;
            switch (deliverType)
            {
                case (int)Dict.InvoiceDeliverType.Send:
                    Meno = com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictInvoiceDeliverType, (int)Dict.InvoiceDeliverType.Send) + "<br /><div class=\"wrap_layout_track\"><a class=\"js_order_address\" href=\"javascript:void(0);\" order_id=\"" + orderId + "\">配送地址<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>配送地址</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div>"; ;
                    break;
                case (int)Dict.InvoiceDeliverType.ToPay:
                    Meno = com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictInvoiceDeliverType, (int)Dict.InvoiceDeliverType.ToPay)+"<br /><div class=\"wrap_layout_track\"><a class=\"js_order_address\" href=\"javascript:void(0);\" order_id=\"" + orderId + "\">配送地址<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>配送地址</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div>";;
                    break;
                case (int)Dict.InvoiceDeliverType.BySelf:
                    Meno = com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictInvoiceDeliverType, (int)Dict.InvoiceDeliverType.BySelf);
                    break;
                default:
                    break;
            }
            return Meno;
        }
        public string ExitBtn(int status, long orderId)
        {
            string Meno = string.Empty;
            switch (status)
            {
                case (int)Dict.InvoiceOrderStatus.UnCheck:
                    //Meno = "<input type=\"button\" value=\"审核通过\" onclick=\"CheckInvoiceOrder('" + orderId + "');\" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"审核不通过\" onclick=\"NoCheckInvoiceOrder('" + orderId + "');\" />";
                    Meno = "<div class=\"wrap_layout_track\"><input type=\"button\" value=\"审核通过\" onclick=\"CheckInvoiceOrder('" + orderId + "');\" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"审核不通过\" class=\"js_invoice_uncheck\" invoice_id=\"" + orderId + "\" onclick=\"javascript:void(0);\" /><div class=\"layout_popup\"><div class=\"layout_hd\"></div><div class=\"layout_inner order_box\"><div class=\"layout_bd\"></div></div></div></div>";

                    break;
                case (int)Dict.InvoiceOrderStatus.CheckFail:
                    Meno = "<input type=\"button\" value=\"审核通过\" onclick=\"CheckInvoiceOrder('" + orderId + "');\" />";

                    break;
                case (int)Dict.InvoiceOrderStatus.CheckOK:
                    Meno = "<div class=\"wrap_layout_track\"><input type=\"button\" value=\"配送\" class=\"js_invoice_deliversuc\" invoice_id=\"" + orderId + "\" onclick=\"javascript:void(0);\" /><div class=\"layout_popup\"><div class=\"layout_hd\"></div><div class=\"layout_inner order_box\"><div class=\"layout_bd\"></div></div></div></div>";

                    break;
                default:
                    break;
            }
            return Meno;
        }
        private string GetPagerUrl()
        {
            string url = "InvoiceList.aspx?";
            url += "page={0}";
            if (queryInvoiceOrderNo != string.Empty)
                url += "&InvoiceorderNo=" + queryInvoiceOrderNo;
            if (queryInvoiceCustName != string.Empty)
                url += "&InvoiceCustName=" + queryInvoiceCustName;
            if (queryInvoiceStart != DateTime.MinValue)
                url += "&InvoiceStart=" + queryInvoiceStart.ToString("yyyy-MM-dd");
            if (queryInvoiceEnd != DateTime.MinValue)
                url += "&InvoiceEnd=" + queryInvoiceEnd.ToString("yyyy-MM-dd");
            if (queryInvoiceOrderType != int.MinValue)
                url += "&InvoiceOrderType=" + queryInvoiceOrderType;
            if (queryInvoiceProName != string.Empty)
                url += "&InvoiceProName=" + queryInvoiceProName;
            return url;
        }
    }
}
